package org.dromara.system.listener;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import io.reactivex.rxjava3.core.Completable;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.excel.core.ExcelListener;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.system.domain.bo.WbCandidateListBo;
import org.dromara.system.domain.bo.WbCompanyBlacklistBo;
import org.dromara.system.domain.bo.WbMinuteListBo;
import org.dromara.system.domain.vo.WbCandidateListVo;
import org.dromara.system.domain.vo.WbCompanyBlacklistVo;
import org.dromara.system.service.IWbCandidateListService;
import org.dromara.system.service.IWbCompanyBlacklistService;
import org.dromara.system.service.IWbCompanyInfoService;
import org.dromara.system.service.IWbProvinceService;

import java.util.List;
import java.util.UUID;

/**
 * 系统用户自定义导入
 *
 * @author Lion Li
 */
@Slf4j
public class WbCandidateListListener extends AnalysisEventListener<WbCandidateListVo> implements ExcelListener<WbCandidateListVo> {

    private final IWbCandidateListService wbCandidateListService;

    private final IWbCompanyInfoService wbCompanyInfoService;

    private Long candidateId ;
    private int successNum = 0;
    private int failureNum = 0;
    private int updateNum = 0;
    private final StringBuilder successMsg = new StringBuilder();
    private final StringBuilder failureMsg = new StringBuilder();

    public WbCandidateListListener(Long candidateId) {
        this.wbCandidateListService = SpringUtils.getBean(IWbCandidateListService.class);
        this.wbCompanyInfoService = SpringUtils.getBean(IWbCompanyInfoService.class);
        this.candidateId = candidateId;
    }

    @Override
    public void invoke(WbCandidateListVo wbCandidateListVo, AnalysisContext context) {
        try {
            //
            if(StringUtils.isNotEmpty(wbCandidateListVo.getSubStandardId()) && StringUtils.isNotEmpty(wbCandidateListVo.getBagNo()) ){
                WbCandidateListBo wbCandidateListBo = BeanUtil.toBean(wbCandidateListVo, WbCandidateListBo.class);
                wbCandidateListBo.setCandidateId(candidateId);

                //只有轮次为第一轮的能够导入
                if("第1轮".equals(wbCandidateListVo.getRound())) {
                    WbCandidateListBo wbCandidateListBo1 = new WbCandidateListBo();
                        wbCandidateListBo1.setCandidateId(candidateId);

                    if (StringUtils.isNotEmpty(wbCandidateListBo.getSubStandardId())){
                        wbCandidateListBo1.setSubStandardId(wbCandidateListBo.getSubStandardId());
                    }

                    if (StringUtils.isNotEmpty(wbCandidateListBo.getSubStandardName())){
                        wbCandidateListBo1.setSubStandardName(wbCandidateListBo.getSubStandardName());
                    }

                    if (StringUtils.isNotEmpty(wbCandidateListBo.getBagNo())){
                        wbCandidateListBo1.setBagNo(wbCandidateListBo.getBagNo());
                    }

                    if (StringUtils.isNotEmpty(wbCandidateListBo.getCandidate())){
                        wbCandidateListBo1.setCandidate(wbCandidateListBo.getCandidate());
                    }

                    List<WbCandidateListVo> wbCandidateListVos = wbCandidateListService.queryList1(wbCandidateListBo1);

                    if (wbCandidateListVos.isEmpty()) {
                        //生成ID ：IdWorker.getId()
                        wbCandidateListBo.setId(IdWorker.getId());
                        wbCandidateListService.insertByBo(wbCandidateListBo);
                        successNum++;
                    }

                    successMsg.append("<br/>").append(successNum).append("、分标编号 ").append(wbCandidateListVo.getSubStandardId()).append(" 导入成功");
                }
            }else{
                failureNum++;
                String msg = "<br/>" + failureNum + "、分标编号 " + wbCandidateListVo.getSubStandardId() + " 导入失败：";
                failureMsg.append(msg).append("无项目ID或文件缺失分标编号");
            }
        } catch (Exception e) {
            failureNum++;
            String msg = "<br/>" + failureNum + "、分标编号 " + wbCandidateListVo.getSubStandardId() + " 导入失败：";
            failureMsg.append(msg).append(e.getMessage());
            log.error(msg, e);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {

    }

    @Override
    public ExcelResult<WbCandidateListVo> getExcelResult() {
        return new ExcelResult<>() {

            @Override
            public String getAnalysis() {
                if (failureNum > 0) {
                    failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                    throw new ServiceException(failureMsg.toString());
                } else {
                    successMsg.insert(0, "恭喜您，数据已全部导入成功！新增共 " + successNum + " ，修改共，"+ updateNum +"条，条，数据如下：");
                }
                return successMsg.toString();
            }

            @Override
            public List<WbCandidateListVo> getList() {
                return null;
            }

            @Override
            public List<String> getErrorList() {
                return null;
            }
        };
    }
}
